**************************
**************************
* Graphs with Panel Data 
**************************
**************************

set more off
use "${FINALDATA}\immo_panel.dta", clear

cap drop __00*

** save graphs to GRAPHS folder:
cd "$GRAPHS\"

** drop observations from last month 
drop if monat == monthly("2015 10","YM") 



***************************
***************************
* Generate variables for graphs
***************************
***************************

** Define time relative to disclosure --> time_disclosure
bysort geo (time): gen count = _n
bysort geo: gen help = count if epc_change == 1
bysort geo (time): egen help2 = max(help)
gen time_disclosure = count - help2 if help2 != .
replace time_disclosure = time_disclosure /* t = 0: disclosure */
drop help help2 count

** Generate variable that gives price change relative to first observation
gen logkaufp_firstobs = log(kaufpreis_firstobs)
gen logkaufp_diff = logkaufp - logkaufp_firstobs

** Generate month of disclosure variable
gen epc_change_month = time if epc_change == 1
bysort geo (epc_change_month): replace epc_change_month = epc_change_month[_n-1] if _n>1

** Generate indicator variable that shows whether an observation is close around the May 2014 or not
bysort geo: egen min_time = min(time)
bysort geo: egen max_time = max(time)
gen around_mai14 = 0
replace around_mai14 = 1 if min_time < `=monthly("2014 05","YM")' & max_time > `=monthly("2014 05","YM")'

** Month of disclosure
gen epc_change_time = time*epc_change 
gsort geo -epc_change_time
bysort geo: replace epc_change_time = epc_change_time[_n-1] if _n > 1

gen epc_post = epc
replace epc_post = 0 if epc_change_time < monthly("2014 05","YM")

** Define compliers
gen complier = epc_change_time != 0



***************************
***************************
* Graphs for paper
***************************
***************************
sort geo time


***************************
** Figure 4 (Left Panel): Distribution of Energy Information Disclosure Dates
***************************
local savename panel_timing_disclosure

local xlin = monthly("2014 05","YM")
histogram time if epc_change == 1, discrete ///
	addplot(pci 0 `xlin' 1600 `xlin') tlabel(2013m5(6)2015m5, format(%tmMonYY) labsize(small)) /* tmtick(2013m1(1)2015m9) */ xtitle("") ///
	ytitle("Number of disclosing sellers") legend(off) ///
	xtitle("Month of disclosure") ///
	graphregion(color(white))  ///
	frequency  ///
	saving("`savename'", replace)
graph export "`savename'.pdf", replace


*******************************************************
** Figure 4 (Left Panel): Price Change Relative to the First Asking Price 
*******************************************************

local savename panel_price_discl

preserve 
	merge 1:1 geo time using "$WORKDATA/result_psmatch.dta", update 
	
	drop if missing(matchid)
	drop _merge
	
	** keep only when a month is available for both matched obs
	bysort matchid time: gen count = _N 
	keep if count == 2
		
	keep if inrange(time_disclosure,-5,3)
	bysort matchid: gen nrobs = _N	
	sum nrobs,d
	keep if nrobs==r(max)
	sum nrobs
	gen aux_price = logkaufp if time_disclosure==-1
	bysort geo: egen aux = mode(aux_price)
	replace aux_price = aux if missing(aux_price)

	gen resid = logkaufp - aux_price
		
	foreach var of numlist 0 1 {
		bysort time_disclosure: egen mean_price_`var' = mean(resid) if complier==`var'  
	}

	table time_disclosure if complier==1, c(mean resid)
	table time_disclosure if complier==0, c(mean resid)

	collapse (mean) mean_price=resid, by(time_disclosure complier)
	local xlin2 = -0.5
	twoway 	(line mean_price time_disclosure if complier==0 & inrange(time_disclosure,-12,6), xline(`xlin2')) ///
			(line mean_price time_disclosure if complier==1 & inrange(time_disclosure,-12,6), lpattern(shortdash)) ///
			, xtitle("Number of months after invidivual disclosure") ytitle("Average difference in log of asking price") yscale(titlegap(*5)) xlabel(#6) legend(rows(2) label(1 "No Disclosure")  label(2 "Disclosure") ring(0) position(8) symxs(*0.4)) ///
			 graphregion(color(white)) saving("`savename'", replace) xlabel(-5(1)3)
		graph export "`savename'.pdf", replace 
restore


************************************
** combine both graphs
************************************

local savename panel_did_graphs

graph combine "panel_timing_disclosure" "panel_price_discl" ///
	, graphregion(color(white)) ///
	ysize(3) iscale(*1.5) ///
	saving("`savename'", replace)
graph export "`savename'.pdf", replace


